package edu.pku.sei.modeler.uml.commands.sequence;

import org.eclipse.draw2d.geometry.Rectangle;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BasicInteractions.DestructionEvent;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Lifeline;
import edu.pku.sei.modeler.uml.commands.NestedElementCreateCommand;

public class UMLStopCreateCommand extends NestedElementCreateCommand {
	private static final int STOP_WIDTH = 20;

	private static final int STOP_HEIGHT = 20;

	private DestructionEvent oldStopEvent;

	public UMLStopCreateCommand(MetaNode node, MetaNode hostNode,
			Rectangle constraint) {
		super(node, hostNode, constraint);
	}

	@Override
	public boolean canExecute() {
		return super.canExecute()
				&& fatherNode.getModelElement() instanceof Lifeline;
	}

	@Override
	protected void updateNodeBounds() {
		int hostX = 0;
		int hostWidth = fatherNode.getWidth();

		hostX += hostWidth / 2;

		// regular operations
		node.setLocationX(hostX - STOP_WIDTH / 2);
		node.setLocationY(fatherNode.getHeight() - STOP_HEIGHT);
		node.setWidth(STOP_WIDTH);
		node.setHeight(STOP_HEIGHT);
	}

	@Override
	protected void undoModelElement() {
		Lifeline lifeline = (Lifeline) fatherNode.getModelElement();
		lifeline.setIncludedStopEvent(this.oldStopEvent);
	}

	@Override
	protected void updateModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		Lifeline lifeline = (Lifeline) fatherNode.getModelElement();
		this.oldStopEvent = lifeline.getIncludedStopEvent();
		lifeline.setIncludedStopEvent((DestructionEvent) modelElement);
	}

}
